import {recognitionLicense} from "../../util/car.js";
// pages/user/car.js
function listCar(that){
  wx.request({
    method: "GET",
    url: getApp().data.servser + '/car/list',
    header: {
      'content-type': 'application/x-www-form-urlencoded',
      'Authorization': 'Bearer ' + wx.getStorageSync('token')
    },
    data: {
      openId: getApp().globalData.openid
    },
    success: function (res) {
      that.setData({
        istrue: false,
        cars: res.data.data.cars
      })
    }
  })
}
exports.listCar = listCar
Page({

  /**
   * 页面的初始数据
   */
  data: {
    istrue: false,
    no: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    listCar(that)
  },
  delete: function (e) {
    let that = this
    wx.showModal({
      title: '提示',
      content: '确认要删除吗',
      success (res) {
        if (res.confirm) {
          wx.request({
            method: "POST",
            url: getApp().data.servser + '/car/delete',
            header: {
              'content-type': 'application/x-www-form-urlencoded',
              'Authorization': 'Bearer ' + wx.getStorageSync('token')
            },
            data: {
              seqId: e.currentTarget.dataset.seqid,
              openId: getApp().globalData.openid
            },
            success: res => {
              that.setData({
                istrue: false,
                cars: res.data.data.cars
              })
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
    
    
  },
  save: function () {
    var that = this;
    if (that.data.no == '') {
      this.setData({
        warnToast: true
      });
      setTimeout(() => {
        this.setData({
          hidewarnToast: true
        });
        setTimeout(() => {
          this.setData({
            warnToast: false,
            hidewarnToast: false,
          });
        }, 300);
      }, 2000);
      return;
    }
    if (this.data.seqId) {
      wx.request({
        method: "POST",
        url: getApp().data.servser + '/car/update',
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Authorization': 'Bearer ' + wx.getStorageSync('token')
        },
        data: {
          no: that.data.no,
          seqId: that.data.seqId,
          openId: getApp().globalData.openid
        },
        success: function (res) {
          wx.showToast({
            title: "更新成功",
            icon: 'none',
            duration: 2000
          })
          that.setData({
            istrue: false,
            cars: res.data.data.cars
          })
        }
      })
    } else {
      wx.request({
        method: "POST",
        url: getApp().data.servser + '/car/add',
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Authorization': 'Bearer ' + wx.getStorageSync('token')
        },
        data: {
          no: that.data.no,
          openId: getApp().globalData.openid
        },
        success: function (res) {
          wx.showToast({
            title: "新增成功",
            icon: 'none',
            duration: 2000
          })
          that.setData({
            istrue: false,
            cars: res.data.data.cars
          })
        }
      })
    }
  },
  update: function (e) {
    console.log(e);
    this.setData({
      istrue: true,
      no: e.currentTarget.dataset.no,
      seqId: e.currentTarget.dataset.seqid
    })
  },
  add: function () {
    let self = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success (res) {
        const tempFilePath = res.tempFilePaths[0];
        wx.showLoading({
          title: '识别中',
        })
        wx.getFileSystemManager().readFile({
          filePath: tempFilePath,
          encoding: "base64",
          success: res => { 
           let base64 = 'data:image/png;base64,' + res.data;
           recognitionLicense(base64).then(res=>{
            let number = res.number;
            self.setData({
              istrue: true,
              no: number
            })
            wx.hideLoading()
           })
          }
        })
      }
    })
    // this.setData({
    //   istrue: true
    // })
  },
  formInputChange(e) {
    this.setData({
      no: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})